﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_RobotOrderDetail : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_RobotOrderDetail()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadSerialList();            
            this.F4RobotOrderDetail.DoubleClick += F4RobotOrderDetail_DoubleClick;
            this.fromSerial.LostFocus += fromSerial_LostFocus;
            //LoadData();
        }

        void fromSerial_LostFocus(object sender, EventArgs e)
        {
            this.toSerial.SelectedIndex = this.fromSerial.SelectedIndex;
        }

        private void LoadSerialList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT SerialNo FROM [231_RobotOrderDetail] ORDER BY SerialNo";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["SerialNo"].ToString().Trim();
                    item.Value = dataReader["SerialNo"].ToString().Trim();
                    this.fromSerial.Properties.Items.Add(item);
                    this.toSerial.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Serial list");
            }
        }

        private void F4RobotOrderDetail_DoubleClick(object sender, EventArgs e)
        {
            CallEditForm();
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            F4RobotOrderDetail.Columns.Clear();
            gridF4RobotOrderDetail.DataSource = null;
            if ((this.fromSerial.SelectedIndex >= 0) && (this.toSerial.SelectedIndex >= 0))
            {
                ComboboxItem from_ser = (ComboboxItem)this.fromSerial.SelectedItem;
                ComboboxItem to_ser = (ComboboxItem)this.toSerial.SelectedItem;
                DataSet sourceDataSet = new DataSet();
                SQLManagement mySQLManagement = new SQLManagement();
                mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
                string sql = "SELECT [SerialNo], [PlanNo], [OrderNo], [CompID], [RepairOrder], [RepairComp], [RobotSerialNo],"
                    + " [Quantity], [Finished], [FinishedDate], [Exported], [ExportedDate], [ExportedBy], [CustomerID], [RoHS],"
                    + " [RJNo], [ErrorNo], [Remark]"
                    + " FROM [231_RobotOrderDetail]"
                    + " WHERE SerialNo BETWEEN '" + from_ser.Value.ToString() + "' AND '" + to_ser.Value.ToString() + "'";
                sourceDataSet = mySQLManagement.SQL_Query(sql);
                if (sourceDataSet != null)
                {
                    gridF4RobotOrderDetail.DataSource = sourceDataSet.Tables[0];
                }
                else
                {
                    DataTable newTable = new DataTable();
                    newTable.Columns.Add("SerialNo", typeof(string));
                    newTable.Columns.Add("PlanNo", typeof(string));
                    newTable.Columns.Add("OrderNo", typeof(string));
                    newTable.Columns.Add("CompID", typeof(string));
                    newTable.Columns.Add("RepairOrder", typeof(string));
                    newTable.Columns.Add("RepairComp", typeof(string));
                    newTable.Columns.Add("RobotSerialNo", typeof(string));
                    newTable.Columns.Add("Quantity", typeof(float));
                    newTable.Columns.Add("Finished", typeof(bool));
                    newTable.Columns.Add("FinishedDate", typeof(DateTime));
                    newTable.Columns.Add("Exported", typeof(string));
                    newTable.Columns.Add("ExportedDate", typeof(DateTime));
                    newTable.Columns.Add("ExportedBy", typeof(string));
                    newTable.Columns.Add("CustomerID", typeof(string));
                    newTable.Columns.Add("RoHS", typeof(bool));
                    newTable.Columns.Add("RJNo", typeof(string));
                    newTable.Columns.Add("ErrorNo", typeof(string));
                    newTable.Columns.Add("Remark", typeof(string));
                    gridF4RobotOrderDetail.DataSource = newTable;
                }
                F4RobotOrderDetail.Columns[0].Caption = "Serial No";
                F4RobotOrderDetail.Columns[1].Caption = "Plan No";
                F4RobotOrderDetail.Columns[2].Caption = "Order No";
                F4RobotOrderDetail.Columns[3].Caption = "Comp ID";
                F4RobotOrderDetail.Columns[4].Caption = "Repair Order";
                F4RobotOrderDetail.Columns[5].Caption = "Repair Comp";
                F4RobotOrderDetail.Columns[6].Caption = "Robot Serial No";
                F4RobotOrderDetail.Columns[7].Caption = "Quantity";
                F4RobotOrderDetail.Columns[8].Caption = "Finished";
                F4RobotOrderDetail.Columns[9].Caption = "Finished Date";
                F4RobotOrderDetail.Columns[10].Caption = "Exported";
                F4RobotOrderDetail.Columns[11].Caption = "Exported Date";
                F4RobotOrderDetail.Columns[12].Caption = "Exported By";
                F4RobotOrderDetail.Columns[13].Caption = "Customer ID";
                F4RobotOrderDetail.Columns[14].Caption = "RoHS";
                F4RobotOrderDetail.Columns[15].Caption = "RJNo";
                F4RobotOrderDetail.Columns[16].Caption = "Error No";
                F4RobotOrderDetail.Columns[17].Caption = "Remark";
                F4RobotOrderDetail.BestFitColumns();
            }
            else
            {
                MessageBox.Show("Invalid Serial");
                this.fromSerial.Focus();
            }
        }

        private void poAdd_Click(object sender, EventArgs e)
        {
            CallEditForm();
        }

        private void CallEditForm()
        {
            if (F4RobotOrderDetail.RowCount > 0)
            {
                DataRow edit_row = F4RobotOrderDetail.GetFocusedDataRow();
                frm_Edit_RobotOrderDetail edit_Form = new frm_Edit_RobotOrderDetail();
                edit_Form.serial = edit_row[0].ToString().Trim();
                if (edit_Form.LoadData())
                {
                    edit_Form.ShowDialog();
                    LoadData();
                }
                else
                {
                    edit_Form.Dispose();
                    MessageBox.Show("Cannot edit selected record");
                }
            }
        }

        private void buttonShow_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
